package com.project.base.baseproject.ModelManager.FileManager;

import android.net.Uri;
import android.os.Environment;

import java.io.File;


/**
 * 文件管理器 所有文件放到一个文件夹里面
 * Created by mac on 2017/5/3.
 */

public class FileManager {
    private static FileManager instance = null;
    public String baseFilePath;//临时文件夹
    public static FileManager manager(){
        if(null == instance)
            instance = new FileManager();
        return instance;
    }
    //根据用户guid加载文件夹 创建默认的文件夹
    public void createFinder() {
        String IMAGE_FILE_DIR = Environment.getExternalStorageDirectory() + "/BaseProject";
        File dir = new File(IMAGE_FILE_DIR);
        if (!dir.exists()) {
            dir.mkdirs();
        }
        File taskDir = new File(IMAGE_FILE_DIR + "/BaseProject");
        if (!taskDir.exists()) {
            taskDir.mkdirs();
        }
        this.baseFilePath = IMAGE_FILE_DIR + "/BaseProject";
    }
    //递归删除文件及文件夹
    public static void delete(File file) {
        if(file == null)
            return;
        if (file.isFile()) {
            file.delete();
            return;
        }
        if(file.isDirectory()){
            File[] childFiles = file.listFiles();
            if (childFiles == null || childFiles.length == 0) {
                file.delete();
                return;
            }
            for (int i = 0; i < childFiles.length; i++) {
                delete(childFiles[i]);
            }
            file.delete();
        }
    }
    //判断文件是否存在 带后缀
    public boolean fileIsExitTask(String fileName) {
        // 得到该路径文件夹下所有的文件
        File mfile = new File(this.baseFilePath);
        File[] files = mfile.listFiles();
        if(files == null)
            return false;
        for (File tempFile : files) {
            String tempFileName = tempFile.getName();
            if(tempFileName.equals(fileName))
                return true;
        }
        return false;
    }
}
